﻿using LiveCharts;
using LiveCharts.Wpf;
using PracticalExamples.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;
using System.Windows.Threading;

namespace PracticalExamples.绘图.ViewModel
{
    public class BrokenLineGraphViewModel:NotifyCommon
    {
        private SeriesCollection _series;
        public SeriesCollection Series {
            get { return _series; }
            set { _series = value; DoNotify(); } }

        private Random random = new Random();

        public BrokenLineGraphViewModel()
        {
            Series = new SeriesCollection() { 
                new LineSeries { Values = new ChartValues<double> { 3, 5, 7, 4, 11, 28, 40, 31 },
                    StrokeDashArray=new DoubleCollection{10 },Fill=new SolidColorBrush(Color.FromArgb(50,225,225,225))},
                new ColumnSeries{ Values = new ChartValues<decimal> { 5, 6, 2, 7 }}
            };

            dynamic();
        }

        private void dynamic()
        {
            DispatcherTimer timer1 = new DispatcherTimer();
            timer1.Interval = TimeSpan.FromSeconds(3);
            timer1.Tick += changSeriesCollection;
            timer1.Start();
        }

        private void changSeriesCollection(object sender, EventArgs e)
        {
            if (Series.Count!=0)
            {
                Series = null;
            }
            Series = new SeriesCollection() {
                
                new ColumnSeries{ Values = new ChartValues<decimal> { 8, 2, 9, 17 }}
            };
            List<double> lineChartValues = new List<double>();
            List<decimal> columnChartValues = new List<decimal>();
            for (int i = 0;i<8;i++)
            {
                lineChartValues.Add(random.Next(0, 50));
                columnChartValues.Add(random.Next(0, 30));
            }

            LineSeries ls = new LineSeries();
            ls.Values = new ChartValues<double>(lineChartValues);
            ls.LineSmoothness = 0;//0: straight lines(直线链接), 1: really smooth lines（平整的）
            ls.DataLabels = true;
            ls.PointGeometrySize = 10;
            ls.LabelPoint = point => point.Y + "mm";
            ls.StrokeDashArray = new DoubleCollection(5);
            ls.Fill = new SolidColorBrush(Color.FromArgb(50, 0, 102, 225));

            ColumnSeries cs = new ColumnSeries();
            cs.Values = new ChartValues<decimal>(columnChartValues);
            cs.Fill = new LinearGradientBrush(Color.FromRgb(51, 51, 153), Color.FromRgb(153, 51, 51), 90);

            Series = new SeriesCollection();
            Series.Add(ls);
            Series.Add(cs);

        }
    }
}
